package com.miquan.minotehelper.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.miquan.minotehelper.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mq on 2015/8/6.
 * --- 流弊 ---
 */
public class ClipImageView extends View {
    private Paint mPaint;

    private List<Point> mPoints = new ArrayList<>();

    private Bitmap mBitmap;

    public ClipImageView(Context context) {
        super(context);
        init();
    }

    public ClipImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClipImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(5);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            mPoints.add(new Point((int) event.getX(), (int) event.getY()));
            postInvalidate();
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.CYAN);
        if (mBitmap != null) {
            canvas.drawBitmap(mBitmap, 0, 0, null);
        }
        drawLines(canvas);
    }

    private void drawLines(Canvas canvas) {
        if (mPoints.size() > 2) {
            for (int i = 0; i < mPoints.size(); i++) {
                Point pPre = mPoints.get(i);
                Point pNext = mPoints.get(i + 1);
                canvas.drawLine(pPre.x, pPre.y, pNext.x, pNext.y, mPaint);

                if (i == mPoints.size() - 2) break;
            }
        }
    }

    public Bitmap getBitmap() {
        Bitmap bmpOld = ImageUtils.getBitmap(this);
        int oldWidth = bmpOld.getWidth();
        int oldHeight = bmpOld.getHeight();

        // 获取最高点和最低点
        int top = 0;
        int bottom = 0;
        int left = 0;
        int right = 0;
        if (mPoints.size() > 2) {
            top = oldHeight;
            left = oldWidth;
            for (Point p : mPoints) {
                if (p.y < top) top = p.y;
                if (p.y > bottom) bottom = p.y;
                if (p.x < left) left = p.x;
                if (p.x > right) right = p.x;
            }
        } else {
            right = oldWidth;
            bottom = oldHeight;
        }
        mPoints.clear();

        int width = right - left;
        int height = bottom - top;

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        Rect src = new Rect(left, top, right, bottom);
        Rect dest = new Rect(0, 0, width, height);
        canvas.drawBitmap(bmpOld, src, dest, null);

        // --- test ---
//        setBitmap(bitmap);

        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        if (mBitmap != null) mBitmap.recycle();
        mBitmap = bitmap;
        postInvalidate();
    }
}
